package com.pet.platform.controller;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.ServiceDTO;
import com.pet.platform.dto.ServiceQueryDTO;
import com.pet.platform.entity.Service;
import com.pet.platform.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/services")
public class ServiceController {
    
    @Autowired
    private ServiceService serviceService;
    
    @GetMapping("/{id}")
    public Result<Service> findById(@PathVariable Long id) {
        return Result.success(serviceService.findById(id));
    }
    
    @GetMapping
    public Result<PageResult<ServiceDTO>> findPage(ServiceQueryDTO queryDTO) {
        return Result.success(serviceService.findPage(queryDTO));
    }
    
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> create(@Valid @RequestBody ServiceDTO serviceDTO) {
        serviceService.create(serviceDTO);
        return Result.success();
    }
    
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> update(@PathVariable Long id, @Valid @RequestBody ServiceDTO serviceDTO) {
        serviceService.update(id, serviceDTO);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> delete(@PathVariable Long id) {
        serviceService.delete(id);
        return Result.success();
    }
    
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        serviceService.updateStatus(id, status);
        return Result.success();
    }
} 